/**
  **************************************************************************
  * @file     main.c
  * @version  v2.0.0
  * @date     2020-11-02
  * @brief    main program
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to 
  * download from Artery official website is the copyrighted work of Artery. 
  * Artery authorizes customers to use, copy, and distribute the BSP 
  * software and its related documentation for the purpose of design and 
  * development in conjunction with Artery microcontrollers. Use of the 
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */

#include "at32f435_437_clock.h"
#include "at_surf_f437_board_lcd.h"
#include "at_surf_f437_board_usart.h"
#include "lv_port_disp_template.h"
#include "lv_port_indev_template.h"
#include "at_surf_f437_board_delay.h"
#include "at_surf_f437_board_touch.h"
#include "at32f435_437_board_qspi_flash.h"
#include "at_surf_f437_board_sdram.h"
#include "at32f435_437_board.h"

#include "lv_tick_custom.h"
#include <stdint.h>
#include <string.h>
#include "lvgl.h"
#include <stdio.h>
#include "gui_guider.h"
#include "events_init.h"
#include "custom.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "bsp.h"


#define USING_EXT_FLASH 1
lv_ui guider_ui;

TaskHandle_t LVGLTask_Handler;
TaskHandle_t LED0Task_Handler;

QueueHandle_t buzzQueue; //define a queue handler


  
static void adcUpdateCbk(void)
{
  int adcValue = analogRead();
  lv_label_set_text_fmt(guider_ui.mainscreen_adValueLbl, "%04d", adcValue);
}



void LVGLTask(void *pvParameters)
{
    /* for littlevgl gui tick increase */  
  tmr7_int_init(287, 999);  //1 micro second interrupt
  
  lcd_init(LCD_DISPLAY_HORIZONTAL);

  touch_init(TOUCH_SCAN_HORIZONTAL);
  lcd_clear(BLACK);
  lv_init();
  lv_port_disp_init();
  lv_port_indev_init();
  //sdram_init();
  //setup_ui(&guider_ui);
  //events_init(&guider_ui);
  custom_init(&guider_ui);
  
  lv_timer_t* task_timer = lv_timer_create((lv_timer_cb_t)adcUpdateCbk, 1000, 0); // create a timer task with interval 1000ms
  if (task_timer == NULL)
  {
      printf("[%s:%d] lv_timer_create failed\n", __FUNCTION__, __LINE__);
  }

  while(1)
  {
    lv_task_handler(); 
    vTaskDelay(10);
  }
} 

/* LED0任务函数 */
void led_task(void *pvParameters)
{
    while(1)
    {
      at32_led_toggle(LED3);
      printf("LED3 Toggle\r\n");
      vTaskDelay(1000);
    }
}

/* key task */
void key_task(void *pvParameters)
{
  uint8_t ucKeyCode;
  int toneTable[] = {0,523,0,0,587,0,0,659,0,0,698,0,0,784,0,0,880,0,0,988,0,0,1047,0,0};
  
    while(1)
    {
      ucKeyCode = bsp_GetKey();      
      if (ucKeyCode != KEY_NONE)
      {
        xQueueSend(buzzQueue,&toneTable[ucKeyCode],0);
        switch (ucKeyCode)
        {
          case KEY_DOWN_K1:
					printf("KEY1 is short pressed\r\n");
					break;				
				
          case KEY_DOWN_K2:
					printf("KEY2 is short pressed\r\n");

					break;
				
          case KEY_LONG_K1:
					printf("KEY1 is long pressed\r\n");
					break;	
				
				default:                     
					break;
        }
    } 
    vTaskDelay(10);
  }
}


/* key task */
void buzz_task(void *pvParameters)
{  
  BaseType_t xResult;
  int toneSet;
  crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
  gpio_init_type gpio_init_struct;
  gpio_default_para_init(&gpio_init_struct);
  
  gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
  gpio_init_struct.gpio_pins = GPIO_PINS_12;//buzz
  gpio_init(GPIOB, &gpio_init_struct);
  gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE12, GPIO_MUX_2);  
  timer5_init();//for buzz pulse out
  while(1)
  {
    xResult = xQueueReceive(buzzQueue,  (void *)&toneSet,  (TickType_t)portMAX_DELAY);
    
    tone(toneSet); 
    vTaskDelay(100);
    toneOff();    
  }  
}
///////////////////////////////////////////////////////////////////
/**
  * @brief  main function.
  * @param  none
  * @retval none
  */


int main(void)
{
	uint16_t i;
  nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);  
  system_clock_config();
  bsp_Init();
  
 #if USING_EXT_FLASH
  ext_flash_init();
 #endif  
  uart_print_init(115200);
  
  buzzQueue = xQueueCreate( 10, sizeof(int));  // creat the queue, the queue length is 10,the queue item size is 1
  if(buzzQueue!=NULL)
    printf("buzzQueue create sucessed  \r\n");
  else 
    printf("buzzQueue create failed \r\n");

    /* 创建LVGL任务 */
  xTaskCreate((TaskFunction_t )LVGLTask,
              (const char*    )"start_task",
              (uint16_t       )512,
              (void*          )NULL,
              (UBaseType_t    )1,
              (TaskHandle_t*  )&LVGLTask_Handler); 
           
              
    xTaskCreate((TaskFunction_t )led_task,     	
              (const char*    )"led_task",   	
              (uint16_t       )128, 
              (void*          )NULL,				
              (UBaseType_t    )2,	
              (TaskHandle_t*  )&LED0Task_Handler); 
           
    xTaskCreate((TaskFunction_t )key_task,     	
              (const char*    )"key_task",   	
              (uint16_t       )128, 
              (void*          )NULL,				
              (UBaseType_t    )3,	
              (TaskHandle_t*  )&LED0Task_Handler); 
              
    xTaskCreate((TaskFunction_t )buzz_task,     	
              (const char*    )"buzz_task",   	
              (uint16_t       )128, 
              (void*          )NULL,				
              (UBaseType_t    )4,	
              (TaskHandle_t*  )&LED0Task_Handler); 


  vTaskStartScheduler();
}



